/*
 * @Date: 2025-07-15 18:06:17
 * @Auth: 463997479@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-07-18 16:07:23
 * @FilePath: \corpus\src\pages\AuthKnowledge\index.tsx
 */
/*
 * @Date: 2025-07-15 17:44:46
 * @Auth: 463997479@qq.com
 * @LastEditors: 463997479@qq.com
 * @LastEditTime: 2025-07-16 15:47:16
 * @FilePath: \corpus\src\pages\AuthKnowledge\index.tsx
 */
import {
  CheckCircleFilled,
  ClockCircleFilled,
  CloseCircleFilled,
  MinusCircleFilled,
} from '@ant-design/icons';
import { ProColumns, ProTable } from '@ant-design/pro-components';
import { Button, Card, Descriptions } from 'antd';

import { typeEnum } from '@/enum';
import { getKnowledFile } from '@/services/ant-design-pro/corpus';
import hook from './hook';
import EditAuthKnowledge from './EditAuthKnowledge';

const AuthKnowledge: React.FC = () => {
  const { setVisible, knowledgeData } = hook.useContainer();
  const columns: ProColumns<any>[] = [
    {
      dataIndex: 'index',
      valueType: 'indexBorder',
      width: 48,
      title: '序号',
    },
    {
      title: 'id',
      dataIndex: 'id',
      hideInForm: false,
      ellipsis: true,
      search: false,
    },

    {
      title: '基础语料集',
      dataIndex: 'fileName',
      ellipsis: true,
    },

    {
      title: '上传时间',
      dataIndex: 'createTime',
      ellipsis: true,
      search: false,
    },
    {
      title: '上传时间',
      dataIndex: 'time',
      ellipsis: true,
      hideInTable: true,

      valueType: 'dateRange',
      search: {
        transform: (value) => {
          return {
            startTime: value[0],
            endTime: value[1],
          };
        },
      },
    },

    {
      title: '解析状态',
      dataIndex: 'analysisStatus',
      ellipsis: true,
      valueType: 'select',
      valueEnum: {
        '0': {
          text: '失败',
          status: 'Error',
        },
        '1': {
          text: '解析向量化中',
          status: 'Pending',
        },
        '2': {
          text: ' 解析向量化成功',
          status: 'Success',
        },
        '3': {
          text: '待解析向量化',
          status: 'Pending',
        },
      },
      render: (_, record: any) => [
        <span key={1}>
          {record?.analysisStatus === 1 ? (
            <ClockCircleFilled style={{ color: 'green' }} />
          ) : record?.analysisStatus === 2 ? (
            <CheckCircleFilled style={{ color: 'green' }} />
          ) : record?.analysisStatus === 3 ? (
            <MinusCircleFilled style={{ color: '#1890ff' }} />
          ) : (
            <CloseCircleFilled style={{ color: 'red' }} />
          )}
          <span style={{ marginLeft: '5.0px' }}>{typeEnum[record?.analysisStatus + '']}</span>
        </span>,
      ],
    },

    {
      title: '操作',
      width: 162,
      dataIndex: 'option',
      valueType: 'option',
      render: (_, record: any) => [
        <a
          key={1}
          onClick={() => {
            window.open(
              `${window.location.origin}/corpus/knowledgeFile/downloadText?fileId=${record?.id}`,
            );
          }}
        >
          下载文件
        </a>,
      ],
    },
  ];
  return (
    <>
      <Card title="知识库上传信息" bordered={false}>
        <Descriptions size="middle" bordered>
          <Descriptions.Item span={2} label="申请人">
            {knowledgeData.applyUserName}
          </Descriptions.Item>
          <Descriptions.Item span={2} label="所属部门">
            {knowledgeData.knowledgeDeptName}
          </Descriptions.Item>
          <Descriptions.Item span={2} label="知识库名称">
            {knowledgeData.knowledgeName}
          </Descriptions.Item>
          <Descriptions.Item span={2} label="知识库描述">
            {knowledgeData.knowledgeDescribe}
          </Descriptions.Item>
          <Descriptions.Item span={3} label="申请人联系电话">
            {knowledgeData.applyUserPhone}
          </Descriptions.Item>
          <Descriptions.Item span={3} label="备注">
            {knowledgeData.remark}
          </Descriptions.Item>
        </Descriptions>
      </Card>
      <div style={{ marginTop: '20.0px' }}>
        <ProTable<API.RuleListItem, API.PageParams>
          toolBarRender={() => [
            <Button
              onClick={() => {
                setVisible(true);
              }}
              type="primary"
              key="show"
            >
              审核
            </Button>,
            <Button
              onClick={() => {
                history.go(-1);
              }}
              key="out"
            >
              返回
            </Button>,
          ]}
          rowKey="key"
          request={async (
            // 第一个参数 params 查询表单和 params 参数的结合
            // 第一个参数中一定会有 pageSize 和  current ，这两个参数是 antd 的规范
            params: any,
          ) => {
            // 这里需要返回一个 Promise,在返回之前你可以进行数据转化
            // 如果需要转化参数可以在这里进行修改

            const msg = await getKnowledFile({
              ...params,
              knowledgeId: knowledgeData.knowledgeId,
              // type: type === 'gr' ? DeptTypeEnum.GR_TO_BM : DeptTypeEnum.BM_TO_GG,
            });
            return {
              data: msg.data,
              // success 请返回 true，
              // 不然 table 会停止解析数据，即使有数据
              success: true,
              // 不传会使用 data 的长度，如果是分页一定要传
              total: msg.total,
            };
          }}
          search={false}
          pagination={{
            showSizeChanger: true,
            defaultPageSize: 10,
          }}
          columns={columns}
          headerTitle="预览文件"
        />
      </div>
      {/* backdrop-filter */}
      {/* </Card> */}
      <EditAuthKnowledge />
    </>
  );
};
export default () => {
  return (
    <hook.Provider>
      <AuthKnowledge />
    </hook.Provider>
  );
};
